package me.ivenapp.gomoku;

import me.ivenapp.gomoku.component.GridLayout;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.global.configuration.Configuration;

/**
 * Slice基类，主要限制了主内容区的宽度
 */
public abstract class SliceBase extends AbilitySlice {
    private GridLayout.GridLayoutConfig layoutConfig;
    private Component layoutCmt;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        if(getAbility() instanceof AbilityBase) {
            AbilityBase ability = (AbilityBase) getAbility();
            //向Ability注册配置变更事件
            ability.setConfigurationUpdatedListener(newConfig -> {
                setGridLayout(layoutCmt);
                onConfigurationUpdated(newConfig);
            });
        }
    }

    /**
     * 设置页面为栅格布局
     * @param layout 栅格布局中主内容组件
     */
    protected void setGridLayout(Component layout) {
        if(layout != null) {
            layoutCmt = layout;
            GridLayout manager = new GridLayout(this);
            int padding;
            if(layoutConfig == null) {
                padding = layoutCmt.getPaddingLeft();
            } else {
                padding = layoutConfig.padding;
            }
            layoutConfig = manager.createGridLayoutConfig(DisplayUtil.getRealSize(this).getPointXToInt(), padding, 0);
            manager.setLayoutConfig(layoutCmt, layoutConfig);
        }
    }

    protected GridLayout.GridLayoutConfig getGridLayoutConfig() {
        return layoutConfig;
    }

    protected void onConfigurationUpdated(Configuration newConfig) {
    }

    /**
     * 获取当前颜色模式。0-深色模式，1-浅色模式
     * @return
     */
    public int getCurrentColorMode() {
        int colorMode = getColorMode();
        if(colorMode == -1) {
            colorMode = getResourceManager().getConfiguration().getSystemColorMode();
        }
        return colorMode;
    }
}

